package ig.metrica.escenario;


import java.sql.SQLException;
import java.lang.Exception;
import ig.da.abs.AbstractBasicDao;
import ig.metrica.escenario.DetalleescenarioDTO;

/**
 * GenJ 2011
 * @author usuario
 *
 * Is an Class for table DB for DAO
 */
public class DetalleescenarioDAO extends AbstractBasicDao{


	// -----------------------------------------------------------------
	// Constants
	// -----------------------------------------------------------------

	// -----------------------------------------------------------------
	// Attributes
	// -----------------------------------------------------------------
    private java.sql.Connection connection = null;

	// -----------------------------------------------------------------
	// Constructors
	// -----------------------------------------------------------------
    public DetalleescenarioDAO ()  {

    }

	// -----------------------------------------------------------------
	// All Methods
	// -----------------------------------------------------------------

                                    

    /**
     * Method for create an record of table of table
     * @return : the pk of table
     * @param dto
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public  DetalleescenarioDTO  create(DetalleescenarioDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;

        try{
            String QUERY = "INSERT INTO DETALLEESCENARIO ( IDESCENARIO, LINEA, CONTENIDO, TIPO, DIRECTORIO ) VALUES ( ?,  ?,  ?,  ?,  ? )";

            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getIdescenario());
            prep.setInt (2,dto.getLinea());
            prep.setString (3,dto.getContenido());
            prep.setString (4,dto.getTipo());
            prep.setString (5,dto.getDirectorio());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return dto;
    }


    /**
     * Update by Pk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int update (DetalleescenarioDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE DETALLEESCENARIO SET IDESCENARIO = ?, LINEA = ?, CONTENIDO = ?, TIPO = ?, DIRECTORIO = ? WHERE IDESCENARIO = ?, LINEA = ?, CONTENIDO = ?, TIPO = ?, DIRECTORIO = ?";
            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getIdescenario());
            prep.setInt (2,dto.getLinea());
            prep.setString (3,dto.getContenido());
            prep.setString (4,dto.getTipo());
            prep.setString (5,dto.getDirectorio());

            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Delete by Pk
     * @param pk
     * @return the number record delete
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int delete (DetalleescenarioDTO dto ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM DETALLEESCENARIO WHERE IDESCENARIO = ?, LINEA = ?, CONTENIDO = ?, TIPO = ?, DIRECTORIO = ?";
            prep = connection.prepareStatement( QUERY );
            
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Find by Pk
     * @param pk
     * @return DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public DetalleescenarioDTO findByPk (DetalleescenarioDTO dto ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        DetalleescenarioDTO detalleescenario = null;
        try{
            String QUERY = "SELECT IDESCENARIO, LINEA, CONTENIDO, TIPO, DIRECTORIO FROM DETALLEESCENARIO WHERE IDESCENARIO = ?, LINEA = ?, CONTENIDO = ?, TIPO = ?, DIRECTORIO = ?";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();
            if (res.next()) {
                detalleescenario = new DetalleescenarioDTO(  res.getInt ( 1 ), res.getInt ( 2 ), res.getString ( 3 ), res.getString ( 4 ), res.getString ( 5 ));
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,res,connection);
        }
        return detalleescenario;
    }

    /**
     * Find by All
     * @return the list DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public java.util.List<DetalleescenarioDTO> findAll() throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<DetalleescenarioDTO> list=new java.util.ArrayList<DetalleescenarioDTO>();

        try{
            String QUERY = "SELECT IDESCENARIO, LINEA, CONTENIDO, TIPO, DIRECTORIO FROM DETALLEESCENARIO ";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();

            while (res.next()) {
                list.add(new DetalleescenarioDTO(  res.getInt ( 1 ), res.getInt ( 2 ), res.getString ( 3 ), res.getString ( 4 ), res.getString ( 5 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    public java.util.List< DetalleescenarioDTO > createAll(java.util.List<DetalleescenarioDTO> dtos) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;
        java.util.List< DetalleescenarioDTO > list=new java.util.ArrayList< DetalleescenarioDTO >();
        try{
            String QUERY = "INSERT INTO DETALLEESCENARIO ( IDESCENARIO, LINEA, CONTENIDO, TIPO, DIRECTORIO ) VALUES ( ?,  ?,  ?,  ?,  ? )";
            for(DetalleescenarioDTO dto : dtos ){

                prep = connection.prepareStatement(QUERY);
                prep.setInt (1,dto.getIdescenario());
                prep.setInt (2,dto.getLinea());
                prep.setString (3,dto.getContenido());
                prep.setString (4,dto.getTipo());
                prep.setString (5,dto.getDirectorio());
                prep.executeUpdate();
                list.add(dto);
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return list;
    }






    /**
     * Open the connection
     * @return
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private java.sql.Connection open() throws ClassNotFoundException, java.sql.SQLException,Exception {
    	return getConnection("METRICADW");
    }

    

     /**
     * The List By idescenario
     * @param idescenario
     * @return list of DetalleescenarioDTO
     */
    public DetalleescenarioDTO findByIdescenario (Integer  linea, String directorio) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();

        try{
            String QUERY = "SELECT IDESCENARIO, LINEA, CONTENIDO, TIPO, DIRECTORIO FROM DETALLEESCENARIO WHERE DIRECTORIO = ?  AND LINEA = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setObject(1,directorio);
            prep.setObject(2,linea);
            res = prep.executeQuery();
            if (res.next()) {
                return new DetalleescenarioDTO(  res.getInt ( 1 ), res.getInt ( 2 ), res.getString ( 3 ), res.getString ( 4 ), res.getString ( 5 ));
            }
            return null;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }
    
     /**
     * The List By idescenario
     * @param idescenario
     * @return list of DetalleescenarioDTO
     */
    public java.util.List<DetalleescenarioDTO> findByIdescenario (Integer  idescenario) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<DetalleescenarioDTO> list=new java.util.ArrayList<DetalleescenarioDTO>();

        try{
            String QUERY = "SELECT IDESCENARIO, LINEA, CONTENIDO, TIPO, DIRECTORIO FROM DETALLEESCENARIO WHERE IDESCENARIO = ? ORDER BY LINEA";
            prep = connection.prepareStatement( QUERY );
            prep.setObject(1,idescenario);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new DetalleescenarioDTO(  res.getInt ( 1 ), res.getInt ( 2 ), res.getString ( 3 ), res.getString ( 4 ), res.getString ( 5 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    public int update(Integer idEscenario, String directorio) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE DETALLEESCENARIO SET IDESCENARIO = ? WHERE DIRECTORIO = ?";
            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,idEscenario);
            prep.setString (2,directorio);
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }

    }

}
    